package com.newtouch.avmarketing.api.service.impl;

import com.newtouch.avmarketing.api.service.Content;
import com.newtouch.avmarketing.api.service.DoubleStepService;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.entity.DoubleConfiguration;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.service.DoubleConfigurationService;
import com.newtouch.avmarketing.mongo.po.PolicyPo;
import com.newtouch.avmarketing.mongo.service.InsurancePolicyService;
import javazoom.jl.decoder.JavaLayerException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileNotFoundException;
import java.util.*;

@Service
public class DoubleStepServiceImpl implements DoubleStepService {

    @Autowired
    private DoubleConfigurationService doubleConfigurationService;
    @Autowired
    private InsurancePolicyService insurancePolicyService;



    /**
     * 公用查询识别内容方法
     * @param id
     * @return
     */
    private DoubleConfiguration cha(Long id){
        DoubleConfiguration doubleConfiguration = doubleConfigurationService.findByIds(id);
        return doubleConfiguration;
    }

    @Override
    public String textToSpeech(Long id,String policyCode) {
        String content = null;
        DoubleConfiguration doubleConfiguration = doubleConfigurationService.findByIds(id);
        String s1 = doubleConfiguration.getStepContent();
        PolicyPo policyPo = insurancePolicyService.findPolicy(policyCode);
        String [] str = {"30","31","32","55","89","96"};


        if (id == 1){
            String[] strings = s1.split("#name");
            String s2 = strings[0];//尊敬的
            String s3 = strings[1];//先生/女生：------
            String[] ages = s3.split("/");
            String name = policyPo.getOwner_name();
            String gender = policyPo.getOwner_node().getGender();
            String s4 = ages[0];//先生
            String s5 = ages[1];//女士：-----
            String[] nicks = s5.split("：");
            String s6 = nicks[0];//女士
            String s7 = nicks[1];//:-----------
            if ("1".equals(gender)){
                content = s2+name+s4+s7;
            }
            if("2".equals(gender)){
                content = s2+name+s5;
            }else if ("string".equals(gender)){
                content = s2+name+s4+s7;
            }
            return content;
        }
        if (id == 2){
            String [] strings = s1.split("/");//  请代理人 / 工作人员向您出示证件，并在镜头前展示三秒。
            String s2 = strings[0];
            String s3 = strings[1];
            String []ss = s2.split("代");
            String s4 = ss[0];
            String s5 = ss[1];
            String [] string = s3.split("向");//  工作人员 向您出示证件，并在镜头前展示三秒。
            String s6 = string[0];
            String s7 = string[1];
            //30、31、32、55、89、96
            if (Arrays.asList(str).contains(policyPo.getSale_channel())){
                content = s4+s3;
            }else {
                content = s2+"向"+s7;
            }
            return content;
        }
        if (id == 3){
            String distributor_name = policyPo.getDistributor_name();
            String distributorCode = policyPo.getDistributorCode();
            if (Arrays.asList(str).contains(policyPo.getSale_channel())){
                String strs = "为您提供保险服务的是#name工作人员，工作人员向您出示的是其本人的证件吗？";
                content =strs.replace("#name",distributor_name);
            }else {
                //拼接销售人员姓名  员工号
                content = s1.replace("#distributor_name",distributor_name).replace("#distributorCode",distributorCode);
            }
            return content;
        }
        if (id == 4){
            content = s1;
            return content;
        }
        if (id == 5){
            //TODO
            String ploicyName = "个人人身险";
            String jiaofeifangshi = policyPo.getPayment().getFirst_paymentMethod();
            String jiaofeiqijian = "12个月";
            String baoxianqijian = "12个月";
            String baofei = "1500元";
            String total = policyPo.getTotalAmount();
            content = s1.replace("#ploicyName",ploicyName).replace("#fangshi",jiaofeifangshi).replace("#jiaofeiqijian",jiaofeiqijian)
                    .replace("#qijian",baoxianqijian)
                    .replace("$baofei",baofei)
                    .replace("#total",total);
            return content;
        }
        if (id == 6){
            //TODO
//            ArrayList<P10SubmitCoverage_node> policyPos = policyPo.getCoverage();
//
//            for (int i = 0; i < policyPos.size(); i++) {
//                System.out.println("name: "+policyPos.get(i).getLifeInsuranceLiabilities().get(i).getLiabilityName());
//            }
            List<String> productStr = new ArrayList<>();

            productStr.add("个人人身险");
            productStr.add("意外险");
            String ploicyName = Arrays.asList(productStr).toString();
            content = s1.replace("#ploicyName",ploicyName);
            return content;
        }
        if (id == 7){
            content = s1;
            return content;
        }
        if (id == 8){
            //TODO
            List<String> productStr = new ArrayList<>();

            productStr.add("个人人身险");
            productStr.add("意外险");
            String ploicyName = Arrays.asList(productStr).toString();
            content = s1.replace("#ploicyName",ploicyName).replace("E","180");
            return content;
        }
        if (id == 9){
            //TODO
            List<String> productStr = new ArrayList<>();
            productStr.add("个人人身险");
            productStr.add("意外险");

            String ploicyName = Arrays.asList(productStr).toString();
            content = s1.replace("#ploicyName",ploicyName);
            return content;
        }
        if (id == 10){
            //TODO
            List<String> productStr = new ArrayList<>();
            productStr.add("个人人身险");
            productStr.add("意外险");
            String ploicyName = Arrays.asList(productStr).toString();

            content = s1.replace("#ploicyName",ploicyName)
                    .replace("X","30")
                    .replace("Y","2000");
            return content;
        }
        if (id == 11){
            content = s1;
            return content;
        }
        if (id == 12){
            content = s1;
            return content;
        }
        if (id == 13){
            content = s1;
            return content;
        }
        if (id == 14){
            content = s1;
            return content;
        }
        return null;
    }

    /**
     *
     * @param id  节点id
     * @param content  语音识别内容
     * @return
     * @throws FileNotFoundException
     * @throws JavaLayerException
     */
    @Override
    public Map<String, String> oneStep(Long id, String content) throws Exception{
        //识别内容
        String [] strings = Content.TONGYI.getName().split("/");
        String suffix = strings[0];
        Map<String, String> map = new HashMap<>();
        if(!StringUtils.isEmpty(content) && suffix.equals(content)){
            map.put("id","2");
        }else {
            map.put("id","1");
            map.put("flag","true");
            map.put("content",cha(id).getNotTishi());
        }
        return map;
    }

    @Override
    public Map<String, String> twoStep(Long id) throws Exception{
        Map<String, String> map = new HashMap<>();
        map.put("id","3");
        map.put("flag","true");
        return map;
    }

    @Override
    public Map<String, String> threeStep(Long id, String content) throws Exception{
        //识别内容
        String [] strings = Content.SHIDE.getName().split("/");
        String suffix = strings[0];

        Map<String, String> map = new HashMap<>();

        if(!StringUtils.isEmpty(content) && suffix.equals(content)){
            map.put("id","4");
        }else {
            map.put("id","3");
            map.put("flag","true");
            map.put("content",cha(id).getNotTishi());
        }
        return map;
    }

    @Override
    public Map<String, String> fourStep(Long id, String content) throws Exception{
        //识别内容
        String [] strings = Content.QINGCHU.getName().split("/");
        String suffix = strings[0];

        Map<String, String> map = new HashMap<>();

        if(!StringUtils.isEmpty(content) && suffix.equals(content)){
            map.put("id","5");
        }else {
            map.put("id","4");
            map.put("flag","true");
            map.put("content",cha(id).getNotTishi());
        }
        return map;
    }

    @Override
    public Map<String, String> fiveStep(Long id, String content) throws Exception{
        //识别内容
        String [] strings = Content.QINGCHU.getName().split("/");
        String suffix = strings[0];

        Map<String, String> map = new HashMap<>();

        if(!StringUtils.isEmpty(content) && suffix.equals(content)){
            map.put("id","6");
        }else {
            map.put("id","5");
            map.put("flag","true");
            map.put("content",cha(id).getNotTishi());
        }
        return map;
    }

    @Override
    public Map<String, String> sixStep(Long id, String content) throws Exception{
        //识别内容
        String [] strings = Content.QINGCHU.getName().split("/");
        String suffix = strings[0];

        Map<String, String> map = new HashMap<>();

        if(!StringUtils.isEmpty(content) && suffix.equals(content)){
            map.put("id","7");
        }else {
            map.put("id","6");
            map.put("flag","true");
            map.put("content",cha(id).getNotTishi());
        }
        return map;
    }

    @Override
    public Map<String, String> sevenStep(Long id, String content) throws Exception{
        //识别内容
        String [] strings = Content.QINGCHU.getName().split("/");
        String suffix = strings[0];//清楚

        Map<String, String> map = new HashMap<>();

        if(!StringUtils.isEmpty(content) && suffix.equals(content)){
            map.put("id","8");
        }else {
            map.put("id","7");
            map.put("flag","true");
            map.put("content",cha(id).getNotTishi());
        }
        return map;
    }

    @Override
    public Map<String, String> eightStep(Long id, String content) throws Exception{
        //识别内容
        String [] strings = Content.QINGCHU.getName().split("/");
        String suffix = strings[0];//清楚

        Map<String, String> map = new HashMap<>();

        if(!StringUtils.isEmpty(content) && suffix.equals(content)){
            map.put("id","9");
        }else {
            map.put("id","8");
            map.put("flag","true");
            map.put("content",cha(id).getNotTishi());
        }
        return map;
    }

    @Override
    public Map<String, String> nineStep(Long id, String content) throws Exception{
        //识别内容
        String [] strings = Content.QINGCHU.getName().split("/");
        String suffix = strings[0];//清楚

        Map<String, String> map = new HashMap<>();

        if(!StringUtils.isEmpty(content) && suffix.equals(content)){
            map.put("id","10");
        }else {
            map.put("id","9");
            map.put("flag","true");
            map.put("content",cha(id).getNotTishi());
        }
        return map;
    }

    @Override
    public Map<String, String> tenStep(Long id, String content) throws Exception{
        //识别内容
        String [] strings = Content.QINGCHU.getName().split("/");
        String suffix = strings[0];//清楚

        Map<String, String> map = new HashMap<>();

        if(!StringUtils.isEmpty(content) && suffix.equals(content)){
            map.put("id","11");
        }else {
            map.put("id","10");
            map.put("flag","true");
            map.put("content",cha(id).getNotTishi());
        }
        return map;
    }

    @Override
    public Map<String, String> elevenStep(Long id, String content) throws Exception{
        //识别内容
        String [] strings = Content.QUEREN.getName().split("/");
        String suffix = strings[0];//清楚

        Map<String, String> map = new HashMap<>();

        if(!StringUtils.isEmpty(content) && suffix.equals(content)){
            map.put("id","12");
        }else {
            map.put("id","11");
            map.put("flag","true");
            map.put("content",cha(id).getNotTishi());
        }
        return map;
    }

    @Override
    public Map<String, String> twelveStep(Long id, String content) throws Exception{
        Map<String, String> map = new HashMap<>();
        map.put("id","13");
        map.put("flag","true");
        return map;
    }

    @Override
    public Map<String, String> thirteenStep(Long id, String content) throws Exception{
        Map<String, String> map = new HashMap<>();
        map.put("id","14");
        map.put("flag","true");
        return map;
    }

    @Override
    public Map<String, String> fourteenStep(Long id, String content) throws Exception{
        //识别内容
        String [] strings = Content.SHIDE.getName().split("/");
        String suffix = strings[0];//清楚

        Map<String, String> map = new HashMap<>();

        if(!StringUtils.isEmpty(content) && suffix.equals(content)){
            map.put("message","双录完成");
        }else {
            map.put("id","14");
            map.put("flag","true");
            map.put("content",cha(id).getNotTishi());
        }
        return map;
    }
}
